import { Toast } from 'antd-mobile';
import React, { Component } from 'react'
import Header from '../../components/Header/Header';
import { reqcartadd, reqgetgoodsinfo } from '../../http/api';
import { $pre } from '../../http/http';
import "./Detail.less"

export class Detail extends Component {
  constructor(){
    super()
    this.state = {
      good:{}
    }
  }
  componentDidMount(){
    let {id} = this.props.match.params;
    this.getgoodsinfo(id);
  }
  async getgoodsinfo(id){
    let res = await reqgetgoodsinfo({id});
    if(res.data.code===200){
      this.setState({
        good:res.data.list[0]
      })
    }
  }
  async addshop(){
    let res = await reqcartadd({
      uid:JSON.parse(sessionStorage.getItem("isLogin")).uid,
      goodsid:this.props.match.params.id,
      type:1,
      num:1
    })
    if(res.data.code === 200){
      Toast.show({
        content:res.data.msg
      })
    }
  }
  render() {
    let {good} = this.state;
    return (
      <div className='detail'>
        <Header title="商品详情" back></Header>
        <img src={$pre+good.img} alt="" />
        <h2>{good.goodsname}</h2>
        <div className='price'>原价：<del>￥{good.market_price}</del></div>
        <div className='price'>现价：￥{good.price}</div>
        <div className="footer">
          <span className='toshop' onClick={()=>this.props.history.push("/index/shop")}>前往购物车</span>
          <span className='addshop' onClick={()=>this.addshop()}>加入购物车</span>
        </div>
      </div>
    )
  }
}

export default Detail